package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.mapper.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//接收参数
@RestController// = @Controller + @ResponseBody
public class UserController {

    @Autowired
    private UserManager userManager;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    //get delete 请求  直接取参数
    //post  put  请求  使用@RequestBody从请求体中取

    //用户保存,返回主键
    @PostMapping("/save")
    public Long save(@RequestBody User user) {
        //调用manager保存
        return userManager.save(user);
    }

    //根据手机号查询用户
    @GetMapping("/findByPhone")
    public User findByPhone(String phone) {
        //调用manager保存
        return userManager.findByPhone(phone);
    }

    //发送手机验证码
    @PostMapping("/user/login")
    public void sendSmsCode(@RequestBody Map<String, String> map) {
        //1. 接收参数
        String phone = map.get("phone");
        //2. 调用manager
        userManager.sendSmsCode(phone);
    }

    //注册登录
    @PostMapping("/user/loginVerification")
    public Map loginRegister(@RequestBody Map<String, String> map) {
        //1. 接收参数
        String phone = map.get("phone");
        String verificationCode = map.get("verificationCode");

        //2. 调用manager
        return userManager.loginRegister(phone, verificationCode);
    }

    //完善个人基本信息
    @PostMapping("/user/loginReginfo")
    public void saveBaseInfo(@RequestBody UserInfo userInfo) {
        //1. 根据token获取用户信息
        User user = UserHolder.getUser();

        //2. 将用户的id设置到UserInfo
        userInfo.setId(user.getId());

        //3. 调用manager保存
        userManager.saveBaseInfo(userInfo);
    }

    //完善个人的头像信息
    @PostMapping({"/user/loginReginfo/head", "/users/header"})
    public void savePhotoInfo(MultipartFile headPhoto) throws IOException {
        //1. 根据token获取用户信息
        User user = UserHolder.getUser();

        //2. 调用manager进行更新
        userManager.savePhotoInfo(user.getId(), headPhoto);
    }

    //查询个人信息
    @GetMapping("/users")
    public UserInfoVo findUserInfo(Long userID, Long huanxinID) {
        //如果userID不为空,就按照userID查询
        if (userID != null) {
            return userManager.findById(userID);
        }
        //如果userID为空,huanxinID不为空,就按照huanxinID查询
        if (huanxinID != null) {
            return userManager.findById(huanxinID);
        }
        //如果上面两个都为空,就从token中获取登录用户id查询
        User user = UserHolder.getUser();
        return userManager.findById(user.getId());
    }

    //修改个人信息
    @PutMapping("/users")
    public void updateBaseInfo(@RequestBody UserInfo userInfo) {

        //调用manager执行修改
        userManager.updateBaseInfo(userInfo);
    }

    //上报个人位置
    @PostMapping("/baidu/location")
    public void saveUserLocation(@RequestBody Map<String,String> map){
       //1.获取参数
        Double latitude = Double.parseDouble(map.get("latitude"));//纬度
        Double longitude = Double.parseDouble(map.get("longitude"));//经度
        String addr = map.get("addrStr");//位置
        //2 调用manager 保存位置
        userManager.saveUserLocation(latitude,longitude,addr);
    }

    //发送验证码
    @PostMapping("/users/phone/sendVerificationCode")
    public void sendVerificationCode() {
        String phone = UserHolder.getUser().getPhone();
        //调用manager
        userManager.sendSmsCode(phone);
    }

    @PostMapping("/users/phone/checkVerificationCode")
    public boolean checkCode(@RequestBody Map<String,String> map) {
        String phone = UserHolder.getUser().getPhone();
        //调用manager
        String verificationCode = map.get("verificationCode");
        return userManager.checkCode(phone,verificationCode);
    }

    @PostMapping("/users/phone")
    public void updatePhone(@RequestBody String phone){
        userManager.updatePhoneByUserId(phone);
    }
}